package com.sxt.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.sys.domain.Role;
import com.sxt.sys.mapper.RoleMapper;
import com.sxt.sys.service.RoleService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.vo.RoleVo;
@Service
public class RoleServiceImpl implements RoleService{
	@Autowired
	private RoleMapper roleMapper;
	
	
	@Override
	public DataGridView queryAllRole(RoleVo RoleVo) {
		Page<Object> page = PageHelper.startPage(RoleVo.getPage(),RoleVo.getLimit());
		List<Role> data = roleMapper.queryAllRole(RoleVo);
		return new DataGridView(page.getTotal(),data);
	}


	@Override
	public Integer addRole(RoleVo roleVo) {
		// TODO Auto-generated method stub
		return roleMapper.insertSelective(roleVo);
	}


	@Override
	public Integer deleteRole(Integer id) {
		// TODO Auto-generated method stub
		return roleMapper.deleteByPrimaryKey(id);
	}


	@Override
	public Integer updateRole(RoleVo roleVo) {
		// TODO Auto-generated method stub
		return roleMapper.updateByPrimaryKeySelective(roleVo);
	}


	@Override
	public List<Role> queryAllRoleForlist(RoleVo roleVo) {
		// TODO Auto-generated method stub
		return roleMapper.queryAllRole(roleVo);
	}


	@Override
	public Integer saveRolePermisison(RoleVo roleVo) {
		Integer rid=roleVo.getId();
		Integer[] pids=roleVo.getIds();
		//1,根据rid删除角色和权限的关系
		this.roleMapper.deleteRolePermissionByRid(rid);
		//2,保存角色和权限的关系
		if(null!=pids&&pids.length>0) {
			for (Integer pid : pids) {
				this.roleMapper.saveRolePermission(rid,pid);
			}
		}
		return 1;
	}


	@Override
	public List<Role> queryAllRoleByUserId(Integer id) {
		// TODO Auto-generated method stub
		return roleMapper.queryAllRoleByUserId(id);
	}


}
